package com.hhsjdesign.test.act;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hhsj.design.pullableview.ability.IPullAble;
import com.hhsj.design.pullableview.recyclerview.RefreshRecycleViewPro;
import com.hhsjdesign.headerpullrefresh.R;
import com.hhsjtest.scaffold.ListScaffoldView;
import com.hhsjtest.scaffold.RecyclerScaffoldView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
    private RefreshRecycleViewPro listView;
    private MAdapter mAdapter;
    private TextView paramsView;
    private int i = 1;
    private RecyclerScaffoldView scaffold;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setTitle("RecyclerView测试");


        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        setContentView(R.layout.act_refresh_recycler);
        scaffold = findViewById(R.id.scaffold);


        TextView topNavigation = new TextView(this);
        topNavigation.setHeight(168);
        topNavigation.setText("顶部导航");
        topNavigation.setGravity(Gravity.CENTER_VERTICAL);
        topNavigation.setPadding(30, 0, 0, 0);
        scaffold.addTopNavigationView(topNavigation);

        scaffold.setFloatBarAction(ListScaffoldView.SCROLL_TO_TOP);
        ImageView floatImage = new ImageView(this);
        floatImage.setImageResource(R.drawable.progressbar_cycle);
        scaffold.addFloatBarView(floatImage);

        scaffold.setFloatBarViewMargin(50, 50);

        listView = findViewById(R.id.scaffold_scrollview);

        paramsView = findViewById(R.id.params);
        paramsView.setText(getText());





        listView.setRefreshEnable(true);
        listView.setLoadMoreEnable(true);

        TextView header = new TextView(this);
        header.setHeight(300);
        header.setBackgroundColor(0xFFFFFFFF);
        header.setText("header1");
        header.setGravity(Gravity.CENTER);


        TextView header2 = new TextView(this);
        header2.setText("footer1");
        header2.setHeight(200);
        header2.setGravity(Gravity.CENTER);


        listView.addHeaderView(header);
//        recyclerView.addFooterView(header2);


        /**
         * 允许展示底线
         */
        listView.setFooterLineEnable(true);
//        recyclerView.addFootLineView(null);

        listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_MID);

        listView.setPullAbleListener(new IPullAble.RefreshLoadMoreListener() {
            @Override
            public void doRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listView.stopRefresh();
                    }
                }, 3_000);
            }

            @Override
            public void doLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        recyclerView.showFootLineViewIfCannotLoadMore();
                        listView.stopLoadMore();
                    }
                }, 3_000);
            }
        });


//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, RecyclerView.VERTICAL));

//        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        listView.setLayoutManager(new GridLayoutManager(this, 3, RecyclerView.VERTICAL, false));

        listView.setAdapter(new RecyclerViewActivity.MAdapter(this));

    }

    private void testContinue() {
        List<String> numbers = new ArrayList<>();
        final String ZERO = "0";
        int i = 0;
        while (i < 600_000) {
            numbers.add(i + "");
            i++;
        }
        for (i = 0; i < numbers.size(); ) {
            if (numbers.get(i).contains(ZERO)) {
                numbers.remove(i);
                continue;
            }
            i++;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
            case R.id.close_elastic:
                listView.setElasticEnable(false);
                break;

            case R.id.open_elastic:
                listView.setElasticEnable(true);
                break;

            case R.id.close_refresh:
                listView.setRefreshEnable(false);
                break;

            case R.id.open_refresh:
                listView.setRefreshEnable(true);
                break;

            case R.id.elaix_top:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_TOP);
                break;

            case R.id.elaix_mid:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_MID);
                break;

            case R.id.close_load_more:
                listView.setLoadMoreEnable(false);
                break;

            case R.id.open_load_more:
                listView.setLoadMoreEnable(true);
                break;

            case R.id.close_footer_line:
                listView.setFooterLineEnable(false);
                break;

            case R.id.open_footer_line:
                listView.setFooterLineEnable(true);
                break;

            case R.id.close_continue_pull:
                listView.setContinuePullEnable(false);
                break;

            case R.id.open_continue_pull:
                listView.setContinuePullEnable(true);
                break;

            case R.id.footerline_hide:
                listView.showFooterLine(false);
                break;

            case R.id.footerline_show:
                listView.showFooterLine(true);
                break;

        }
        paramsView.setText(getText());
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private String getText() {
        StringBuilder builder = new StringBuilder();

        builder.append("弹性功能：").append(listView.isElasticEnable())
                .append("\n").append("弹性位置：").append(listView.getRefreshViewMode())
                .append("\n").append("下拉刷新：").append(listView.isRefreshEnable())
                .append("\n").append("继续下拉：").append(listView.isContinuePullEnable())
                .append("\n").append("上拉加载：").append(listView.isLoadMoreEnable())
                .append("\n").append("底线功能：").append(listView.isFooterLineEnable())
                .append("\n").append("底线展示：").append(listView.isFooterLineShowing())
        ;

        return builder.toString();
    }



    static class MAdapter extends RecyclerView.Adapter<RecyclerViewActivity.MAdapter.MHolder> {

        private Context context;

        MAdapter(Context context) {
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerViewActivity.MAdapter.MHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            TextView textView = new TextView(context);
            textView.setMinHeight(100);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(new RefreshRecycleViewPro.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new RecyclerViewActivity.MAdapter.MHolder(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerViewActivity.MAdapter.MHolder holder, int position) {
            ((TextView) holder.itemView).setText(position + "");
            holder.itemView.setBackgroundColor((int) ((-1 * Math.random()) * 0xFFFFFFF));
        }

        @Override
        public int getItemCount() {
            return 102;
        }

        @Override
        public int getItemViewType(int position) {
            return position % 3 + 1;
        }

        static class MHolder extends RecyclerView.ViewHolder {

            MHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }

}
